﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;

namespace coffeeshop.Models
{
    public class User
    {
        private static UserTableAdapter userAdapter = new UserTableAdapter();

        public string Name { get; set; }
        public string Password { get; set; }
        public string Role { get; set; }
        public int UserRoleId { get; set; }
        public int UserId { get; private set; }
        public string Title { get; set; }
        public bool IsActive { get; set; }

        public User() { }
        public User(int userId) { UserId = userId; }

        public User Login()
        {
            User result = new User();
            coffeeDataSet.UserDataTable userData = userAdapter.Login(Name, Password);

            if (userData.Any())
            {
                result.UserId = (int)userData.Rows[0]["UserId"];
                result.Name = (string)userData.Rows[0]["Name"];
                result.Password = (string)userData.Rows[0]["Password"];
                result.Title = (string)userData.Rows[0]["Title"];
                
            }

            return result;
        }

        public static User GetById(int userId)
        {
            User result = new User();
            coffeeDataSet.UserDataTable userData = userAdapter.GetById(userId);

            if (userData.Any())
            {
                result.UserId = (int)userData.Rows[0]["UserId"];
                result.Name = (string)userData.Rows[0]["Name"];
                result.Password = (string)((userData.Rows[0]["Password"] is DBNull) ? string.Empty : userData.Rows[0]["Password"]);
                result.Title = (string)userData.Rows[0]["Title"];
                result.UserRoleId = (int)userData.Rows[0]["UserRoleId"];
            }

            return result;
        }

        public static coffeeDataSet.UserDataTable LoadUsers(bool toHideInactiveUsers)
        {
            return userAdapter.GetUserList(toHideInactiveUsers);
        }

        public bool Save()
        {
            try
            {
                int result;
                if (UserId == 0)
                    result = userAdapter.AddUser(Name, Password, Title, UserRoleId); 
                else
                    result = userAdapter.UpdateUser(Name, Password, Title, UserRoleId, UserId);
                
                return result == 1;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateActiveness()
        {
            try
            {
                int result = userAdapter.UpdateActiveness(IsActive, UserId);

                return result == 1;
            }
            catch
            {
                return false;
            }
        }

        public static coffeeDataSet.UserDataTable GetThuNgan()
        {
            return userAdapter.GetEmployees((int)Global.UserRoleId.Cashier);
        }

        public static coffeeDataSet.UserDataTable GetPhucVu()
        {
            return userAdapter.GetEmployees((int)Global.UserRoleId.Waiter);
        }
    }
}
